package com.knife.bbs.business;

import java.util.ArrayList;
import java.util.List;

import com.knife.bbs.object.BBSDoc;
import com.knife.bbs.object.Config;

public class ConfigService extends DAOSupportService {
	private static ConfigService singleton=new ConfigService();
	public static ConfigService getInstance()
	{
		return singleton;
	}
	
	public List<Config> getConfigs()
	{
		String sql="1=1";
		return queryConfig(sql);
	}
	
	public Config getConfig()
	{
		com.knife.bbs.domain.Config skinObj = (com.knife.bbs.domain.Config)this.dao.get(com.knife.bbs.domain.Config.class,1);
		if(skinObj!=null){
			return new Config(skinObj); 
		}else{
			return null;
		}
	}
	
	public Config getConfigBySN(String sn)
	{
		com.knife.bbs.domain.Config skin = (com.knife.bbs.domain.Config)this.dao.getBy(com.knife.bbs.domain.Config.class,"sn",sn);
		if(skin!=null){
			return new Config(skin); 
		}else{
			return null;
		}
	}
	
	public Config getUsedConfig(){
		com.knife.bbs.domain.Config skinObj = (com.knife.bbs.domain.Config)this.dao.getBy(com.knife.bbs.domain.Config.class,"status",1);
		if(skinObj!=null){
			return new Config(skinObj); 
		}else{
			return null;
		}
	}
	
	public List<Config> queryConfig(String scope)
	{		
		return changeListType(this.dao.query(com.knife.bbs.domain.Config.class,scope));	
	}
	
	public BBSDoc getDirLastTopic(String sn)
	{
		java.util.Collection<Object> paras=new java.util.ArrayList<Object>();
		paras.add(sn);
		java.util.List<Object> list = this.dao.query(com.knife.bbs.domain.BBSDoc.class,"sn=? and status>=0 and (parentId is null or (parentId='')) order by inputTime desc",paras,0,1);
		com.knife.bbs.domain.BBSDoc doc=null;
		if(list!=null && list.size()>0)doc=(com.knife.bbs.domain.BBSDoc)list.get(0);
		if(doc!=null){
			return new BBSDoc(doc);
		}else{
			return null;
		}
	}
	
	public List<BBSDoc> getDirLastTopics(String sn,int num)
	{
		List<BBSDoc> results=new ArrayList<BBSDoc>();
		java.util.Collection<Object> paras=new java.util.ArrayList<Object>();
		paras.add(sn);
		java.util.List<Object> list = this.dao.query(com.knife.bbs.domain.BBSDoc.class,"sn=? and status>=0 and (parentId is null or (parentId='')) order by inputTime desc",paras,0,num);
		for(Object adoc:list){
			com.knife.bbs.domain.BBSDoc rdoc=(com.knife.bbs.domain.BBSDoc)adoc;
			BBSDoc doc = new BBSDoc(rdoc);
			results.add(doc);
		}
		return results;
		/*
		com.knife.bbs.domain.BBSDoc doc=null;
		if(list!=null && list.size()>0)doc=(com.knife.bbs.domain.BBSDoc)list.get(0);
		if(doc!=null){
			return new BBSDoc(doc);
		}else{
			return null;
		}*/
	}
	
	public boolean update(Config aobj)
	{
		com.knife.bbs.domain.Config obj=new com.knife.bbs.domain.Config();
		obj.setConfig(aobj);
		return this.dao.update(obj);
	}
	
	public List<Config> changeListType(List<Object> objs){
		List<Config> results = new ArrayList<Config>();
		if(objs!=null){
			if(objs.size()>0){
				for (Object newobj : objs) {
					com.knife.bbs.domain.Config robj = (com.knife.bbs.domain.Config) newobj;
					Config tobj = new Config(robj);
					results.add(tobj);
				}
			}
		}
		return results;
	}
}
